<template>
  <div class="movie-card" @click="$emit('click')">
    <div class="poster-container">
      <img 
        :src="movie.picPaths || placeholderImage" 
        :alt="movie.title"
        class="poster"
        loading="lazy"
      >
      <div class="rating-badge">
        <el-rate
          v-model="rating"
          disabled
          show-score
          text-color="#ff9900"
          score-template="{value}"
        />
      </div>
    </div>
    <div class="info">
      <h3>{{ movie.title }}</h3>
      <div class="meta">
        <span class="year">{{ displayYear }}</span>
        <span class="actors">{{ truncatedActors }}</span>
      </div>
      <div class="hover-content">
        <p class="description">{{ truncatedDescription }}</p>
        <el-button type="primary" size="small">查看详情</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { computed } from 'vue'

export default {
  props: {
    movie: {
      type: Object,
      required: true
    }
  },
  setup(props) {
    const placeholderImage = 'https://via.placeholder.com/300x450?text=No+Poster'
    
    const rating = computed(() => {
      return props.movie.rating ? parseFloat(props.movie.rating) / 2 : 3.5
    })
    
    const displayYear = computed(() => {
      if (!props.movie.addedTime) return '未知年份'
      return new Date(props.movie.addedTime).getFullYear()
    })
    
    const truncatedActors = computed(() => {
      if (!props.movie.actors) return '演员未知'
      return props.movie.actors.length > 15 
        ? props.movie.actors.substring(0, 15) + '...' 
        : props.movie.actors
    })
    
    const truncatedDescription = computed(() => {
      if (!props.movie.desc) return '暂无简介'
      return props.movie.desc.length > 100 
        ? props.movie.desc.substring(0, 100) + '...' 
        : props.movie.desc
    })
    
    return {
      placeholderImage,
      rating,
      displayYear,
      truncatedActors,
      truncatedDescription
    }
  }
}
</script>

<style scoped>
.movie-card {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  cursor: pointer;
  position: relative;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.movie-card:hover {
  transform: translateY(-10px);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
}

.poster-container {
  position: relative;
  padding-top: 150%;
  overflow: hidden;
}

.poster {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s;
}

.movie-card:hover .poster {
  transform: scale(1.05);
}

.rating-badge {
  position: absolute;
  bottom: 10px;
  left: 10px;
  background: rgba(0, 0, 0, 0.7);
  padding: 5px 10px;
  border-radius: 20px;
  color: white;
  display: flex;
  align-items: center;
}

.info {
  padding: 15px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

h3 {
  margin: 0 0 8px;
  font-size: 16px;
  font-weight: bold;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.meta {
  margin-bottom: 10px;
  font-size: 14px;
  color: #666;
}

.year {
  margin-right: 10px;
}

.hover-content {
  opacity: 0;
  transition: opacity 0.3s;
  margin-top: auto;
}

.movie-card:hover .hover-content {
  opacity: 1;
}

.description {
  font-size: 14px;
  color: #666;
  margin-bottom: 10px;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.el-button {
  width: 100%;
}
</style>